
#include "ch32v10x.h"
#include <rtthread.h>
#include <rthw.h>
#include "drivers/pin.h"

#include "power_bsp.h"

#define PWR_BTN_PIN     34      //PB13
#define PWR_STA_PIN     33      //PB12

#define POWER_COUNTS_MIN            (0)
#define POWER_COUNTS_MID_DOWN       (5)
#define POWER_COUNTS_MID_UP         (25)
#define POWER_COUNTS_MAX            (50)



UINT8 pwr_status = 0;
INT16 pwr_counts = 0;


void pwr_on(void)
{
    rt_pin_write(PWR_BTN_PIN,PIN_LOW);
}
void pwr_off(void)
{
    rt_pin_write(PWR_BTN_PIN,PIN_HIGH);
}

void pwr_sta_loop(void)
{
    if(rt_pin_read(PWR_STA_PIN) == PIN_HIGH)
    {
        if(pwr_counts <= POWER_COUNTS_MAX)
        {
            pwr_counts++;
        }
    }
    else
    {
        if(pwr_counts >= POWER_COUNTS_MIN)
        {
            pwr_counts--;
        }
    }
    if(pwr_counts <= POWER_COUNTS_MIN)
    {
        pwr_status = 0;
    }
    else if(pwr_counts >= POWER_COUNTS_MAX)
    {
        pwr_status = 1;
    }
    else if(pwr_counts >= POWER_COUNTS_MID_DOWN && pwr_counts <= POWER_COUNTS_MID_UP)
    {
        pwr_status = 2;
    }
}


UINT8 pwr_get_status(void)
{
    return pwr_status;
}

void pwr_init(void)
{
    rt_pin_mode(PWR_BTN_PIN,PIN_MODE_OUTPUT);
    rt_pin_mode(PWR_STA_PIN,PIN_MODE_INPUT);
    pwr_off();
}

INIT_COMPONENT_EXPORT(pwr_init);
